using Coding4Fun.Phone.Controls.Helpers;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Shapes;
namespace Coding4Fun.Phone.Controls
{
	public class ColorPicker : ColorBaseControl
	{
		private const string string_0 = "SampleSelector";
		private const string string_1 = "SelectedHueColor";
		private const string string_2 = "ColorSlider";
		private const string string_3 = "Body";
		private bool bool_0;
		private Point point_0;
		protected Grid SampleSelector;
		protected Rectangle SelectedHueColor;
		protected ColorSlider ColorSlider;
		private MovementMonitor movementMonitor_0;
		public static readonly DependencyProperty ThumbProperty = DependencyProperty.Register("Thumb", typeof(object), typeof(ColorPicker), new PropertyMetadata(null));
		public object Thumb
		{
			get
			{
				return base.GetValue(ColorPicker.ThumbProperty);
			}
			set
			{
				base.SetValue(ColorPicker.ThumbProperty, value);
			}
		}
		public ColorPicker()
		{
			base.set_DefaultStyleKey(typeof(ColorPicker));
			base.add_SizeChanged(new SizeChangedEventHandler(this.ColorPicker_SizeChanged));
			base.add_IsEnabledChanged(new DependencyPropertyChangedEventHandler(this.ColorPicker_IsEnabledChanged));
			base.add_Loaded(new RoutedEventHandler(this.ColorPicker_Loaded));
		}
		private void ColorPicker_Loaded(object sender, RoutedEventArgs e)
		{
			this.method_4();
		}
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			this.SampleSelector = (base.GetTemplateChild("SampleSelector") as Grid);
			this.SelectedHueColor = (base.GetTemplateChild("SelectedHueColor") as Rectangle);
			Grid grid = base.GetTemplateChild("Body") as Grid;
			if (grid != null)
			{
				this.movementMonitor_0 = new MovementMonitor();
				this.movementMonitor_0.Movement += new EventHandler<MovementMonitorEventArgs>(this.method_1);
				this.movementMonitor_0.MonitorControl(grid);
			}
			this.ColorSlider = (base.GetTemplateChild("ColorSlider") as ColorSlider);
			if (this.ColorSlider != null)
			{
				if (this.Thumb == null)
				{
					this.Thumb = new ColorSliderThumb();
				}
				this.ColorSlider.ColorChanged += new ColorBaseControl.ColorChangedHandler(this.method_0);
				if (this.SelectedHueColor != null)
				{
					Binding binding = new Binding();
					binding.set_Source(this.ColorSlider);
					binding.set_Path(new PropertyPath("SolidColorBrush", new object[0]));
					Binding binding2 = binding;
					this.SelectedHueColor.SetBinding(Shape.FillProperty, binding2);
				}
			}
		}
		private void ColorPicker_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
		{
			this.method_4();
		}
		private void method_0(object object_0, Color color_0)
		{
			this.method_2();
		}
		private void ColorPicker_SizeChanged(object sender, SizeChangedEventArgs e)
		{
			if (base.Color.get_A() == 0 && base.Color.get_R() == 0 && base.Color.get_G() == 0 && base.Color.get_B() == 0)
			{
				base.Color = this.ColorSlider.Color;
				return;
			}
			this.UpdateLayoutBasedOnColor();
		}
		private void method_1(object sender, MovementMonitorEventArgs e)
		{
			this.point_0.set_X(e.X);
			this.point_0.set_Y(e.Y);
			this.method_2();
		}
		private void method_2()
		{
			this.bool_0 = true;
			this.method_3();
			float saturation = (float)(this.point_0.get_X() / this.SelectedHueColor.get_ActualWidth());
			float value = (float)(1.0 - this.point_0.get_Y() / this.SelectedHueColor.get_ActualHeight());
			base.ColorChanging(ColorSpace.ConvertHsvToRgb(this.ColorSlider.Color.GetHue(), saturation, value));
			this.bool_0 = false;
		}
		private void method_3()
		{
			double actualHeight = this.SampleSelector.get_ActualHeight();
			double actualHeight2 = this.SelectedHueColor.get_ActualHeight();
			double actualWidth = this.SelectedHueColor.get_ActualWidth();
			this.point_0.set_X(ControlHelper.CheckBound(this.point_0.get_X(), actualWidth));
			this.point_0.set_Y(ControlHelper.CheckBound(this.point_0.get_Y(), actualHeight2));
			double num = this.point_0.get_X() - actualHeight;
			double num2 = this.point_0.get_Y() - actualHeight;
			num = ControlHelper.CheckBound(num, actualWidth);
			num2 = ControlHelper.CheckBound(num2, actualHeight2);
			this.SampleSelector.set_Margin(new Thickness(num, num2, 0.0, 0.0));
		}
		protected internal override void UpdateLayoutBasedOnColor()
		{
			if (!this.bool_0 && this.SelectedHueColor != null)
			{
				base.UpdateLayoutBasedOnColor();
				HSV hSV = base.Color.GetHSV();
				if (this.ColorSlider != null)
				{
					this.ColorSlider.Color = ColorSpace.GetColorFromHueValue((float)((int)hSV.Hue));
				}
				this.point_0.set_X((double)hSV.Saturation * this.SelectedHueColor.get_ActualWidth());
				this.point_0.set_Y((double)(1f - hSV.Value) * this.SelectedHueColor.get_ActualHeight());
				this.method_3();
				return;
			}
		}
		private void method_4()
		{
			VisualStateManager.GoToState(this, base.get_IsEnabled() ? "Normal" : "Disabled", true);
		}
	}
}
